package com.demo.spring.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class DemoAspectC implements Ordered {

    private final String thisClassName = this.getClass().getSimpleName();

    @Override
    public int getOrder() {
        return 15;
    }

    @Pointcut("execution(* com.demo.spring.aop.service.impl.DemoServiceImpl.targetMethod(..))" +
            "|| execution(* com.demo.spring.aop.service.impl.DemoServiceImpl.targetMethodException(..))")
    public void pointcut() { }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        log.info("in {}.around, before proceed, ",thisClassName);

        Object returns = pjp.proceed();
        returns = true;

        log.info("in {}.around, after proceed, changed returns={},  ",thisClassName,returns);

        return returns;
    }

    @AfterReturning(value="pointcut()",returning = "returning")
    public Object afterReturning(JoinPoint joinPoint,Object returning){
        log.info("in {}.afterReturning, returning={}",thisClassName,returning);

        return returning;
    }

}
